<?php


namespace Bridge;


class Client
{
    /**
     * 一句话描述: 可以将抽象化部分与实现化部分分开，取消二者的继承关系，改用组合关系, 使类的继承(指数)增长变成(m+n)增长
     * 实现: 抽象系列 setter 实现系列, 抽象系列扩展实现系列的方法
     */
    public static function main()
    {
        $implementor = new ImplementorA();
        $abstraction = new AbstractionA($implementor);
        $abstraction->operation();

        $abstraction = new AbstractionB(new ImplementorB());
        $abstraction->operation();
    }
}


interface Implementor
{
    public function operation();
}

class ImplementorA implements Implementor
{

    public function operation()
    {
        echo '具体实现化角色A', PHP_EOL;
    }
}

class ImplementorB implements Implementor
{

    public function operation()
    {
        echo '具体实现化角色B', PHP_EOL;
    }
}

abstract class Abstraction
{
    protected Implementor $implementor;

    public function __construct(Implementor $implementor)
    {
        $this->implementor = $implementor;
    }

    abstract public function operation();
}

class AbstractionA extends Abstraction
{

    public function operation()
    {
        echo '---AAA--- start:', PHP_EOL;
        $this->implementor->operation();
        echo '---AAA--- end. :', PHP_EOL;
    }
}

class AbstractionB extends Abstraction
{

    public function operation()
    {
        echo '---BBB--- start:', PHP_EOL;
        $this->implementor->operation();
        echo '---BBB--- end. :', PHP_EOL;
    }
}


Client::main();
